﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.FlowStep
{
    public partial class ProPartymemberInspection : PageBase
    {
        public string ID
        {
            get
            {
                return GetQueryIntValue("id");
            }
        }
        public string RelativeMeritsContent { get; set; }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
            var ps = DB.PartyJoinProcesss.Find(this.CurrentStepID);
            if (ps != null)
                this.lbWorkGuide.Text = ps.WorkPoint ?? "";
        }

        private void BindData()
        {
        }
        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(this.CurrentStandID))
            {
                string MemberID = PartyProbationaryPartyMember.GetMemberIDByStandID(this.CurrentStandID);
                if (!string.IsNullOrEmpty(MemberID))
                {
                    this.hdfMemberID.Value = MemberID;
                    var oModel = DB.PartyProbationaryPartyMembers.Find(MemberID);
                    if (oModel != null)
                    {
                        this.txtUserName.Text = oModel.UserName;
                        this.txtUnitsAndPost.Text = oModel.UnitsAndPost;
                        this.ddlGender.SelectedValue = oModel.Gender;
                        if (oModel.PassTime != null)
                        {
                            this.dtPassTime.Text = string.Format("{0:yyyy-MM-dd}", oModel.PassTime);
                        }
                        if (oModel.AppTime != null)
                        {
                            this.dtAppTime.Text = string.Format("{0:yyyy-MM-dd}", oModel.AppTime);
                        }
                        if (oModel.TimeExpiration != null)
                        {
                            this.dtTimeExpiration.Text = string.Format("{0:yyyy-MM-dd}", oModel.TimeExpiration);
                        }
                        this.ppOU.Value = oModel.OUID;
                        this.ppOU.Text = PartyOrg.QueryBranchName(oModel.OUID);
                        this.ppOU.Enabled = false;
                        this.RelativeMeritsContent = oModel.RelativeMerits;
                    }
                }
                else
                {
                    var appMode = DB.PartyJoinApplycation.Find(this.AppllicationID);
                    if (appMode != null)
                    {
                        var om = DB.PartyOrgMembers.Find(appMode.LoginID);
                        this.txtUserName.Text = appMode.UserName;
                        this.ppOU.Value = appMode.BranchID;
                        this.ppOU.Text = PartyOrg.QueryBranchName(appMode.BranchID);
                        this.ppOU.Enabled = false;
                        this.ddlGender.SelectedValue = om.Gender;
                        var le = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                        if (le != null)
                        {
                            DataSet ds = PartyProbationaryPartyMember.GetTime(le.AppllicationID);
                            if (ds.Tables.Count > 1)
                            {
                                this.dtPassTime.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["MeetingTime"]).ToString("yyyy-MM-dd");
                                this.dtAppTime.Text = Convert.ToDateTime(ds.Tables[1].Rows[0]["MeetingTime"]).ToString("yyyy-MM-dd");
                                this.dtTimeExpiration.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["MeetingTime"]).AddYears(1).ToString("yyyy-MM-dd");
                            }
                        }
                    }
                }
            }
            //页面只读显示
            if (this.ReadOnly.Trim() == "1")
            {
                this.btnSubmit.Visible = false;
                this.btnSaveClose.Visible = false;
                //if (IsJoinPartyAdmin)
                //{
                //    this.btnSaveClose.Visible = true;
                //}
            }
        }



        private void SaveData(bool isClose, bool isSubmit)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过

            bool bResult = false;
            if (!string.IsNullOrEmpty(this.CurrentStandID))
            {
                string MemberID = this.hdfMemberID.Value.Trim();
                PartyProbationaryPartyMember member = new PartyProbationaryPartyMember();
                if (!string.IsNullOrEmpty(MemberID))
                {
                    member = DB.PartyProbationaryPartyMembers.Find(MemberID);
                }
                member.UserName = this.txtUserName.Text.Trim();
                member.Gender = this.ddlGender.SelectedValue.Trim();
                member.UnitsAndPost = this.txtUnitsAndPost.Text.Trim();
                member.PassTime = Convert.ToDateTime(this.dtPassTime.Text.Trim());
                member.AppTime = Convert.ToDateTime(this.dtAppTime.Text.Trim());
                member.TimeExpiration = Convert.ToDateTime(this.dtTimeExpiration.Text.Trim());
                member.OUID = this.ppOU.Value;
                member.RelativeMerits = this.Request.Form["editor"];
                if (!string.IsNullOrEmpty(MemberID))
                {
                    bResult = DB.SaveChanges() > 0;
                }
                else
                {
                    member.MemberID = StringHelper.NewID;
                    member.StandID = this.CurrentStandID;
                    DB.PartyProbationaryPartyMembers.Add(member);
                    bResult = DB.SaveChanges() > 0;
                }

                if (bResult)
                {
                    if (isSubmit && ReadOnly != "1")
                    {
                        var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                        oModel.Remark = string.Format("填写预备党员考察表完成！", this.CurrentUser.UserName);
                        oModel.HandleUserID = this.CurrentUser.LoginID;
                        oModel.HandleUserName = this.CurrentUser.UserName;
                        oModel.HandleTime = DateTime.Now;
                        oModel.Status = (int)Enum_FlowStanceStatus.通过;

                        bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);
                        if (bResult)
                        {
                            int ii = 3;
                            #region 新增3条考察意见数据
                            if (this.CurrentStepID == ProcesUtil.JoinStep30)
                            {
                                ii = 2;
                            }
                            for (int i = 0; i < ii; i++)
                            {
                                var example = DB.PartyJoinLinkExamples.Find(Convert.ToString(HttpContext.Current.Session["NextStandID"]));
                                PartyJoinApplycation app = ProcesUtil.GetApplycation(example.AppllicationID);

                                PartyJoinReviewOption rep = new PartyJoinReviewOption();
                                rep.ID = StringHelper.NewID;
                                rep.StandID = Convert.ToString(HttpContext.Current.Session["NextStandID"]);
                                rep.LoginID = app.LoginID;
                                rep.LoginName = app.UserName;
                                rep.OptionTime = DateTime.Now.AddMonths(i * 6);
                                rep.CreateTime = DateTime.Now;
                                DB.PartyJoinReviewOptions.Add(rep);
                                bResult = DB.SaveChanges() > 0;
                            }
                            HttpContext.Current.Session["NextStandID"] = null;
                            #endregion                            
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(Message))
                                Alert.Show(Message, String.Empty);
                        }
                    }

                }
            }
        }
        #endregion

        #region
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (string.IsNullOrEmpty(this.txtUnitsAndPost.Text))
            {
                Message = "工作单位及职务为空不能提交，请确认！";
                Alert.Show(Message, String.Empty);
                return;
            }
            if (string.IsNullOrEmpty(FilterSpecialCharacters.Filter(this.Request.Form["editor"])))
            {
                Message = "被批准为预备党员时的主要优缺点不能为空，请确认！";
                Alert.Show(Message, String.Empty);
                return;
            };
            this.SaveData(true, true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(true, false);
            Alert.Show("保存成功！", String.Empty);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}